﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Daycare.Models;
using Daycare.Services;

namespace Daycare
{
    public partial class Scheduling : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lblStartDate.Text = DateTime.Now.ToShortDateString();
                lblEndDate.Text = " - " + DateTime.Now.ToShortDateString();

                SetScheduleDate(ddlScheduleViews.SelectedValue, 0);
            }
        }

        protected void InitializeControls()
        {

        }

        protected void SetScheduleDate(String scheduleView, int dateOffset)
        {
            DateTime currentStartDate;
            DateTime.TryParse(lblStartDate.Text, out currentStartDate);
            DateTime currentEndDate;
            DateTime.TryParse(lblEndDate.Text.Substring(2), out currentEndDate);

            if (currentStartDate != null && currentEndDate != null)
            {
                if (currentStartDate.DayOfWeek.ToString() == "Saturday")
                {
                    currentStartDate = currentStartDate.AddDays(2);
                }
                else if (currentStartDate.DayOfWeek.ToString() == "Sunday")
                {
                    currentStartDate = currentStartDate.AddDays(1);
                }

                currentEndDate = currentStartDate;

                switch (scheduleView)
                {
                    case "Daily":
                        if (currentStartDate.DayOfWeek.ToString() == "Friday" && dateOffset == 1)
                        {
                            currentStartDate = currentStartDate.AddDays(2);

                        }
                        else if (currentStartDate.DayOfWeek.ToString() == "Monday" && dateOffset == -1)
                        {
                            currentStartDate = currentStartDate.AddDays(-2);
                        }

                        currentEndDate = currentStartDate;

                        lblStartDate.Text = String.Format("{0:MM/dd/yyyy}", currentStartDate.AddDays(dateOffset));
                        lblEndDate.Text = " - " + String.Format("{0:MM/dd/yyyy}", currentEndDate.AddDays(dateOffset));

                        break;
                    case "Weekly":
                        int daysFromMonday = 0;

                        switch (currentStartDate.DayOfWeek.ToString())
                        {
                            case "Monday":
                                daysFromMonday = 0;
                                break;
                            case "Tuesday":
                                daysFromMonday = -1;
                                break;
                            case "Wednesday":
                                daysFromMonday = -2;
                                break;
                            case "Thursday":
                                daysFromMonday = -3;
                                break;
                            case "Friday":
                                daysFromMonday = -4;
                                break;
                        }

                        if (dateOffset == 0)
                        {
                            currentStartDate = currentStartDate.AddDays(daysFromMonday);
                            currentEndDate = currentStartDate.AddDays(4);
                        }
                        else if (dateOffset == -1)
                        {
                            currentStartDate = currentStartDate.AddDays(-7);
                            currentEndDate = currentStartDate.AddDays(4);
                        }
                        else if (dateOffset == 1)
                        {
                            currentStartDate = currentStartDate.AddDays(7);
                            currentEndDate = currentStartDate.AddDays(4);
                        }

                        lblStartDate.Text = String.Format("{0:MM/dd/yyyy}", currentStartDate);
                        lblEndDate.Text = " - " + String.Format("{0:MM/dd/yyyy}", currentEndDate);

                        lblMondayDate.Text = String.Format("{0:MM/dd/yyyy}", currentStartDate.AddDays(0));
                        lblTuesdayDate.Text = String.Format("{0:MM/dd/yyyy}", currentStartDate.AddDays(1));
                        lblWednesdayDate.Text = String.Format("{0:MM/dd/yyyy}", currentStartDate.AddDays(2));
                        lblThursdayDate.Text = String.Format("{0:MM/dd/yyyy}", currentStartDate.AddDays(3));
                        lblFridayDate.Text = String.Format("{0:MM/dd/yyyy}", currentStartDate.AddDays(4));

                        break;
                }
            }

            ltrChildren.Text = GenerateChildrenSchedule(DateTime.Parse(lblStartDate.Text));
            ltrStaff.Text = GenerateStaffSchedule(DateTime.Parse(lblStartDate.Text));
        }

        protected void Unnamed_Click(object sender, EventArgs e)
        {
            mpeCheckin.Show();
        }

        protected void ddlScheduleViews_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetScheduleDate(ddlScheduleViews.SelectedValue, 0);

            pnlDailyHeader.Visible = !pnlDailyHeader.Visible;
            pnlWeeklyHeader.Visible = !pnlWeeklyHeader.Visible;

            String viewType = ddlScheduleViews.SelectedValue;

            foreach (Control co in pnlScheduleChildren.Controls)
            {
                if (co is Panel)
                {
                    foreach (Control subCo in co.Controls)
                    {
                        if (subCo is Panel)
                        {
                            Panel pnl = (Panel)subCo;

                            if (pnl.CssClass.Contains("daily"))
                            {
                                pnl.Visible = (ddlScheduleViews.SelectedValue == "Daily");
                            }
                            else if (pnl.CssClass.Contains("weekly"))
                            {
                                pnl.Visible = (ddlScheduleViews.SelectedValue == "Weekly");
                            }
                        }
                    }
                }
            }

            foreach (Control co in pnlScheduleStaff.Controls)
            {
                if (co is Panel)
                {
                    foreach (Control subCo in co.Controls)
                    {
                        if (subCo is Panel)
                        {
                            Panel pnl = (Panel)subCo;

                            if (pnl.CssClass.Contains("daily"))
                            {
                                pnl.Visible = (ddlScheduleViews.SelectedValue == "Daily");
                            }
                            else if (pnl.CssClass.Contains("weekly"))
                            {
                                pnl.Visible = (ddlScheduleViews.SelectedValue == "Weekly");
                            }
                        }
                    }
                }
            }
        }

        protected void lbDateChange_Click(object sender, EventArgs e)
        {
            LinkButton lbDateChange = (LinkButton)sender;

            int dateOffset = 0;

            switch (lbDateChange.ClientID)
            {
                case "lbPreviousDate":
                    dateOffset = -1;

                    break;
                case "lbNextDate":
                    dateOffset = 1;

                    break;
            }

            SetScheduleDate(ddlScheduleViews.SelectedValue, dateOffset);
        }

        protected void bScheduleDaycare_Click(object sender, EventArgs e)
        {
            mpeSchedule.Show();
        }

        protected void ToggleStaffChild_Click(object sender, EventArgs e)
        {
            LinkButton button = (LinkButton)sender;

            switch (button.ClientID)
            {
                case "lbStaffTab":
                    lbStaffTab.CssClass = "tab selected";
                    lbChildrenTab.CssClass = "tab";

                    pnlScheduleChildren.Visible = false;
                    pnlScheduleStaff.Visible = true;

                    break;
                case "lbChildrenTab":
                    lbStaffTab.CssClass = "tab";
                    lbChildrenTab.CssClass = "tab selected";

                    pnlScheduleChildren.Visible = true;
                    pnlScheduleStaff.Visible = false;

                    break;
            }
        }

        protected void WeeklyHeader_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;

            Label lblDate = new Label();

            foreach(Control co in lb.Controls)
            {
                if(co is Label)
                {
                    lblDate = (Label)co;
                    break;
                }
            }

            if (lblDate.Text != String.Empty)
            {
                DateTime clickedDate = DateTime.Parse(lblDate.Text);
                DateTime mondayDate = DateTime.Parse(lblStartDate.Text);
                
                TimeSpan span = clickedDate.Subtract ( mondayDate); 
                int daysOffset = span.Days;
               
                ddlScheduleViews.SelectedValue = "Daily";

                SetScheduleDate("Daily", daysOffset);
                ddlScheduleViews_SelectedIndexChanged(ddlScheduleViews, new EventArgs());
            }
            else
            {
                return;
            }
        }

        protected String GenerateChildrenSchedule(DateTime date)
        {
            StringBuilder sb = new StringBuilder();

             //<asp:Panel runat="server" ID="pnlChild1" CssClass="divBackground-lightgrey child" Style="margin-bottom: 1px; padding-top: 2px; padding-bottom: 0px;">
             //               <div style="width: 150px; display: inline-block; margin-right: 15px; padding-bottom: 1px; padding-top: 1px" class="clean-gray ellipsisText">
             //                   <asp:LinkButton runat="server" OnClick="Unnamed_Click" Style="color: black">
             //                               <img src="Uploads/Images/t_bob130259983181934626.png" />
             //                               <span class="name">Greg Anderson</span>
             //                   </asp:LinkButton>
             //               </div>
             //               <asp:Panel runat="server" CssClass="daily" Style="display: inline-block; vertical-align: top">
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top; margin-left: 15px" class="divBackgroundUnfulfilledSchedule">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackgroundUnfulfilledSchedule divBackground-scheduleSoloDaily">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackgroundFulfilledSchedule divBackground-scheduleSoloDaily">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackground-scheduleSoloDaily">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackground-scheduleSoloDaily">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackground-scheduleSoloDaily">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackgroundUnfulfilledSchedule divBackground-scheduleSoloDaily">
             //                   </div>
             //               </asp:Panel>
             //               <asp:Panel runat="server" CssClass="weekly" Style="display: inline-block; vertical-align: top" Visible="false">
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top; margin-left: 70px; text-align: center; border: 1px solid black" class="divBackgroundUnfulfilledSchedule">
             //                       <span>Scheduled: 4</span><br />
             //                       <span>Attended: 4</span>
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top; border: 1px solid black" class=" divBackground-scheduleSoloWeekly divBackgroundUnfulfilledSchedule ">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top; border: 1px solid black" class="divBackgroundFulfilledSchedule divBackground-scheduleSoloWeekly">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top; border: 1px solid black" class="divBackground-scheduleSoloWeekly divBackgroundFulfilledSchedule">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top; border: 1px solid black" class="divBackground-scheduleSoloWeekly divBackgroundFulfilledSchedule">
             //                   </div>
             //               </asp:Panel>
             //           </asp:Panel>

            return sb.ToString();
        }

        protected String GenerateStaffSchedule(DateTime date)
        {
            StringBuilder sb = new StringBuilder();

             //<asp:Panel runat="server" ID="pnlStaff1" CssClass="divBackground-lightgrey staff" Style="margin-bottom: 1px; padding-top: 2px; padding-bottom: 0px;">
             //               <div style="width: 150px; display: inline-block; margin-right: 15px; padding-bottom: 1px; padding-top: 1px" class="clean-gray ellipsisText">
             //                   <asp:LinkButton runat="server" OnClick="Unnamed_Click" Style="color: black">
             //                               <img src="Uploads/Images/t_bob130259983181934626.png" />
             //                               <span class="name">Greg Anderson</span>
             //                   </asp:LinkButton>
             //               </div>
             //               <asp:Panel runat="server" CssClass="daily" Style="display: inline-block; vertical-align: top">
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top; margin-left: 15px" class="divBackgroundUnfulfilledSchedule">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackgroundUnfulfilledSchedule divBackground-scheduleSolo">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackgroundFulfilledSchedule divBackground-scheduleSolo">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackground-scheduleSolo">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackground-scheduleSolo">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackground-scheduleSolo">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackgroundUnfulfilledSchedule divBackground-scheduleSolo">
             //                   </div>
             //               </asp:Panel>
             //               <asp:Panel runat="server" CssClass="weekly" Style="display: inline-block; display: none; vertical-align: top">
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top; margin-left: 15px; text-align: center" class="divBackgroundUnfulfilledSchedule">
             //                       <span>Scheduled: 4</span><br />
             //                       <span>Attended: 4</span>
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackgroundUnfulfilledSchedule divBackground-scheduleSolo">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackgroundFulfilledSchedule divBackground-scheduleSolo">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackground-scheduleSolo divBackgroundFulfilledSchedule">
             //                   </div>
             //                   <div style="width: 110px; height: 40px; display: inline-block; vertical-align: top;" class="divBackground-scheduleSolo divBackgroundFulfilledSchedule">
             //                   </div>
             //               </asp:Panel>
             //           </asp:Panel>


            return sb.ToString();
        }


        
    }


    
}